package com.xzmzhp.service.cosmetic.impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageInfo;
import com.xzmzhp.constant.ReturnConstant;
import com.xzmzhp.dao.cosmetic.CosmeticMapper;
import com.xzmzhp.pojo.cosmetic.Cosmetic;
import com.xzmzhp.pojo.cosmetic.CosmeticVO;
import com.xzmzhp.service.cosmetic.CosmeticService;
import com.xzmzhp.utils.return_tool.ReturnData;
import com.xzmzhp.vo.personnel.PersonnelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

/**
 * @author 宋雪
 * @Date: 2020/11/18 18:29
 * @Description: 化妆品表数据逻辑实现类
 */
@Service
public class CosmeticServiceImpl implements CosmeticService {
    @Autowired
    private CosmeticMapper cosmeticMapper;

    /**
     * 添加化妆品
     *
     * @param cosmetic 化妆品信息
     * @return 是否成功
     */
    @Override
    public ReturnData addCosmetic(Cosmetic cosmetic) {
        Integer i = cosmeticMapper.addCosmetic(cosmetic);
        if (i > 0) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "添加化妆品出现错误");
        }
    }

    /**
     * 删除化妆品
     *
     * @param id 化妆品id
     * @return 是否成功
     */
    @Override
    public ReturnData deleteCosmetic(Integer id) {
        Integer i = cosmeticMapper.deleteCosmetic(id);
        if (i > 0) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "删除化妆品出现错误");
        }
    }

    /**
     * 更新化妆品
     *
     * @param cosmetic 化妆品信息
     * @return 是否成功
     */
    @Override
    public ReturnData updateCosmetic(Cosmetic cosmetic) {
        if (cosmetic != null) {
            Integer i = cosmeticMapper.updateCosmetic(cosmetic);
            if (i > 0) {
                return ReturnData.ok();
            }
        }
        return ReturnData.error(ReturnConstant.FAIL, "修改失败");
    }

    /**
     * 查询全部化妆品
     *
     * @return 化妆品信息
     */
    @Override
    public ReturnData selectCosmeticAll() {
        List<CosmeticVO> cosmetics = cosmeticMapper.selectCosmeticAll();
        if (cosmetics != null) {
            PageInfo<CosmeticVO> pageInfo = new PageInfo<>(cosmetics);
            return ReturnData.ok(cosmetics,pageInfo.getTotal());
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "没有找到化妆品");
        }
    }

    /**
     * 根据id查询化妆品信息
     *
     * @param id 化妆品id
     * @return 化妆品信息
     */
    @Override
    public ReturnData selectCosmeticById(Integer id) {
        CosmeticVO cosmetic = cosmeticMapper.selectCosmeticById(id);
        if (cosmetic != null) {
            return ReturnData.ok(cosmetic);
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "没有找到该化妆品");
        }
    }

    @Override
    public List<CosmeticVO> selectCosmeticByParam(Integer cosmeticTypeId, String cosmeticName, BigDecimal maxPrice, BigDecimal minPrice, String remark1) {
        List<CosmeticVO> cosmeticVOList = cosmeticMapper.selectCosmeticByParam(cosmeticTypeId, cosmeticName, maxPrice, minPrice, remark1);
        if (CollUtil.isNotEmpty(cosmeticVOList)) {
            return cosmeticVOList;
        } else {
            return Collections.emptyList();
        }
    }
}
